/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.internal.trees;

import java.util.List;

import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.xored.glance.internal.items.ItemCell;
import com.xored.glance.internal.items.ItemControl;
import com.xored.glance.internal.items.ItemProvider;

/**
 * @author Yuri Strot
 * 
 */
public class TreeControl extends ItemControl {

	public TreeControl(Tree table) {
		super(table);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xored.glance.internal.items.ItemControl#collectCells(java.util.List)
	 */
	@Override
	protected void collectCells(List<ItemCell> cells) {
		Tree tree = (Tree) composite;
		TreeItem[] items = tree.getItems();
		int columns = tree.getColumnCount();
		if (columns == 0)
			columns = 1;
		for (int i = 0; i < items.length; i++) {
			for (int j = 0; j < columns; j++) {
				cells.add(new ItemCell(items[i], j, getItemProvider()));
			}
			collectCells(cells, items[i]);
		}
	}

	protected void collectCells(List<ItemCell> cells, TreeItem item) {
		if (item.getExpanded()) {
			TreeItem[] items = item.getItems();
			int columns = item.getParent().getColumnCount();
			if (columns == 0)
				columns = 1;
			for (int i = 0; i < items.length; i++) {
				for (int j = 0; j < columns; j++) {
					cells.add(new ItemCell(items[i], j, getItemProvider()));
				}
				collectCells(cells, items[i]);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xored.glance.internal.items.ItemControl#getItemProvider()
	 */
	@Override
	protected ItemProvider getItemProvider() {
		return TreeItemProvider.getInstance();
	}

}
